<template>
  <div>
    <Header></Header>
    <div class="archive-header">
      <div class="archive-header-inner h-v-30">
        <div class="bg-effect bg-poster poster-zoom" style="
            background-image: url('https://cosy.demo.nicetheme.xyz/wp-content/uploads/2020/08/2020082617345518.jpg');
          ">
          <div class="overlay-1"></div>
        </div>
        <div class="archive-header-body px-3">
          <span class="btn btn-outline-light btn-xl btn-archive font-theme"><b
              class="h-1x">{{ text == "发表文章" ? text : ruleForm.title }}</b></span>
        </div>
      </div>
    </div>
    <el-row>
      <el-col :span="5">
        <div class="grid-content"></div>
      </el-col>
      <el-col style="margin-top: 48px" :span="14" class="m-content">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="标题" prop="title">
            <el-input v-model="ruleForm.title"></el-input>
          </el-form-item>

          <el-form-item label="摘要" prop="description">
            <el-input type="textarea" v-model="ruleForm.description"></el-input>
          </el-form-item>

          <el-form-item label="图片上传" prop="image">
            <el-upload class="upload-demo" drag :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"
              action="http://localhost:8081/blog/uploadImage" :headers="head" :show-file-list="false">
              <img v-if="ruleForm.image" :src="ruleForm.image" class="avatar" />
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip" slot="tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-form-item>

          <el-form-item label="内容" prop="content">
            <mavon-editor v-model="ruleForm.content"></mavon-editor>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">{{
              text
            }}</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="5">
        <div class="grid-content"></div>
      </el-col>
    </el-row>
    <div class="m-content"></div>
  </div>
</template>

<script>
import Header from "../components/Header";
export default {
  name: "BlogEdit.vue",
  components: { Header },
  data () {
    return {
      file: "",
      ruleForm: {
        id: "",
        title: "",
        description: "",
        content: "",
        image: "",
      },
      text: "",
      head: {
        Authorization: localStorage.getItem("token"),
      },
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
        ],
        description: [
          { required: true, message: "请输入摘要", trigger: "blur" },
          // { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        content: [{ trequired: true, message: "请输入内容", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this;
          this.$axios
            .post("/blog/edit", this.ruleForm, {
              headers: {
                Authorization: localStorage.getItem("token"),
              },
            })
            .then((res) => {
              console.log(res);
              _this.$alert(res.data.msg, "提示", {
                confirmButtonText: "确定",
                callback: (action) => {
                  _this.$router.push("/blogs");
                },
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    },
    handleAvatarSuccess (res, file) {
      this.ruleForm.image = res.data;
      // console.log(this.ruleForm.image);
      this.file = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;
      // if (!isJPG || !isPNG) {
      //   this.$message.error("上传头像图片只能是 JPG 格式!");
      // }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
  },
  created () {
    const blogId = this.$route.params.blogId;
    console.log(blogId);
    const _this = this;
    if (blogId) {
      this.$axios.get("/blog/" + blogId).then((res) => {
        const blog = res.data.data;
        _this.ruleForm.id = blog.id;
        _this.ruleForm.title = blog.title;
        _this.ruleForm.description = blog.description;
        _this.ruleForm.content = blog.content;
        _this.ruleForm.image = blog.image;
        _this.text = "编辑文章";
      });
    } else {
      this.text = "发表文章";
    }
  },
};
</script>

<style scoped>
.m-content {
  text-align: center;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

*,
*::before,
*::after {
  -webkit-box-sizing: inherit;
  box-sizing: inherit;
  -webkit-font-smoothing: antialiased;
  word-break: break-word;
  word-wrap: break-word;
}

.archive-header-inner {
  position: relative;
  overflow: hidden;
  display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  -webkit-flex-direction: column;
  flex-direction: column;
  -webkit-box-flex: 1;
  -ms-flex: 1 1 auto;
  -webkit-flex: 1 1 auto;
  flex: 1 1 auto;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
}
.h-v-30 {
  min-height: 30vh;
}

.poster-zoom {
  -webkit-animation: posterZoom 30s linear;
  animation: posterZoom 30s linear;
  -webkit-animation-fill-mode: forwards;
  animation-fill-mode: forwards;
}
.bg-poster {
  -ms-background-size: cover;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}
.bg-effect {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
.overlay-1 {
  width: 100%;
  height: 100%;
  background-color: rgba(22, 29, 39, 0.88);
  position: absolute;
  opacity: 0.4;
  top: 0;
  left: 0;
  -webkit-transition: opacity 0.3s ease-in-out;
  -o-transition: opacity 0.3s ease-in-out;
  transition: opacity 0.3s ease-in-out;
}
.archive-header-body {
  position: relative;
  z-index: 2;
}
.pl-3,
.px-3 {
  padding-left: 1rem !important;
}
.pr-3,
.px-3 {
  padding-right: 1rem !important;
}

.archive-header-body .btn-xl.btn-archive {
  min-width: 400px;
}
.archive-header-body .btn-xl.btn-archive {
  position: relative;
  background-color: transparent;
  background-image: none;
  border-color: #f4f4f5;
  color: #fff;
  font-size: 1.75rem;
  font-weight: bold;
  padding: 1rem;
  border-radius: 0;
  text-transform: uppercase;
  -webkit-transition: 1s all ease;
  -o-transition: 1s all ease;
  transition: 1s all ease;
}
.nice-style-radius .nav-links > span,
.nice-style-radius .nav-links > a,
.nice-style-radius .btn,
.nice-style-radius .form-control {
  border-radius: 5px;
}
.btn-outline-light {
  color: #515c67;
  background-color: #fff;
  background-image: none;
  border-color: #eeeeef;
}
.btn {
  border-radius: 2px;
  white-space: normal;
  font-size: 1rem;
  padding: 0.625rem 2rem;
}
.btn-outline-light {
  color: #f8f9fa;
  border-color: #f8f9fa;
}
.btn {
  display: inline-block;
  font-weight: 400;
  color: #212529;
  text-align: center;
  vertical-align: middle;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  background-color: transparent;
  border: 1px solid transparent;
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  line-height: 1.5;
  border-radius: 0.25rem;
  transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
    border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}
.font-theme {
  font-family: Manrope, "SF Pro SC", "SF Pro Text", Arial, sans-serif;
}
.archive-header-body .btn-xl.btn-archive::before {
  width: 0%;
  height: 100%;
}
.archive-header-body .btn-xl.btn-archive::before {
  background: #fff;
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  z-index: -1;
  -webkit-transition: all 0.6s ease;
  -o-transition: all 0.6s ease;
  transition: all 0.6s ease;
}
.archive-header-body .btn-xl.btn-archive b {
  position: relative;
  /* display: block; */
  z-index: 2;
  text-align: center;
  letter-spacing: 0.5em;
  text-indent: 0.5em;
}
.h-1x {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
b,
strong {
  font-weight: 700;
}
</style>
